<?php

namespace App\Http\Controllers;

use App\Http\Requests\UserLoginRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SessionController extends Controller
{
    //

    public function __construct()
    {
        //只让未登陆用户能访问登录页
        $this->middleware('guest', [
            'only' => ['create']
        ]);
    }

    public function create()
    {
      return view('session.create');
    }

    public function store(UserLoginRequest $request)
    {
        if(Auth::attempt(['email'=>$request->email,'password'=>$request->password],$request->has('remember'))){
            if(Auth::user()->activated){
                session()->flash('success', '好的成功了');
                return redirect()->route('users.show',[Auth::user()]);
            }else{
                Auth::logout();
                session()->flash('warning', '你的账号未激活，请检查邮箱中的注册邮件进行激活。');
                return redirect('/');
            }

        }else{
            session()->flash('danger', '很抱歉，您的邮箱和密码不匹配');
            return redirect()->back()->withInput();
        }
    }

    public function destroy()
    {
        Auth::logout();
        session()->flash('success','退出成功');
        return redirect()->route('login');
    }
}
